package com.crm.controller;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Askers;
import com.crm.entity.Students;
import com.crm.entity.User;
import com.crm.service.AskersService;
import com.crm.service.StudentsService;
import com.crm.service.UserService;
import com.crm.utils.PageUtil;
import com.crm.utils.ResponeMessageUtil;

@RestController
@RequestMapping("/askers")
@EnableScheduling //启动定时任务
public class AskersController {
	
	@Autowired
	private AskersService askersService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ServletContext servletContext;
	
	@Autowired
	private StudentsService studentsService;
	
	/**
	 * 查询咨询经理里面的咨询师
	 * @param page
	 * @param limit
	 * @param askername
	 * @return
	 */
	@RequestMapping("/selectAskersAll")
	public ResponeMessageUtil selectAskersAll(Integer page, Integer limit, String askername) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(askersService.getCount(askername), askersService.selectAll(setValue, limit, askername));
	}
	
	/**
	 * 修改咨询经理里面的咨询师(修改权重)
	 * @param askers
	 * @return
	 */
	@RequestMapping("/updateAskers")
	public boolean updateAskers(Askers askers) {
		return askersService.update(askers)>0?true:false;
	}
	
	/**
	 * 把user表咨询师插入asker表中
	 * 每隔一小时进行插入
	 */
	@Scheduled(cron = "0 0 * * * ?")//0 0 * * * ?
	public void reportCurrentTime() {
		System.out.println("开始执行");
		List<User> userList = userService.selectAskersAll();
		if (userList.size()>0) {
			for (int i = 0; i < userList.size(); i++) {
				Askers askers = new Askers();
				askers.setAskerid(userList.get(i).getId());
				askers.setAskername(userList.get(i).getLoginName());
				if (askersService.selectIdAndName(userList.get(i).getId(), userList.get(i).getLoginName()) == null) { //user表插入asker表判断是否存在
					askersService.insert(askers);//不存在是进行添加
					System.out.println("添加成功");
				}
				System.out.println("已存在不进行添加");
			}
			System.out.println("执行结束");
		}
	}
	
	/**
	 * 给学生分配咨询师
	 */
	@Scheduled(cron = "0 */5 * * * ?")
	public void reportCurrentCron() {
		System.out.println("----------开启执行自动分配咨询师任务----------");
		String isAuto = (String) servletContext.getAttribute("autoAllotAskerSwitch");
		//判断分量开关是否开启,如果没开,直接return
		if (isAuto != null && isAuto.equals("off")) {
			System.out.println("----------分量开关已关闭，不执行自动分配，请手动分配----------");
			return;
		}
		//查询出所有的当天已签到的咨询师(按权重从大到小排序)
		List<Askers> askersList = askersService.selectCheckedId();
		//查询出所有的待分配的学生(按照创建时间从小到大)
		List<Students> studentsList = studentsService.selectAllotZXS();
		//获取学生人数和咨询师人数
		int xsNum = askersList.size();
		int zxsNum = studentsList.size();
		if (xsNum == 0) {
			System.out.println("没有待分配的学生....");
			return;
		}
		if (zxsNum == 0) {
			System.out.println("没有可用的咨询师....");
			return;
		}
		boolean flag;
		//如果学生人数>咨询师人数
		if (xsNum > zxsNum) {
			//算出总共分配几轮
			int lun = xsNum/zxsNum;
			//算出剩余的学生
			int shengyu = xsNum%zxsNum;
			//循环每一轮,先分配每一轮的学生
			int temp = 0;
			for (int i = 0; i < lun; i++) {
				for (int j = 0; j < zxsNum; j++) {
					studentsList.get(temp).setAskerid(askersList.get(j).getAskerid());
					studentsList.get(temp).setZixunname(askersList.get(j).getAskername());
					temp++;
				}
			}
			//再分配剩余的学生
			for (int i = 0; i < shengyu; i++) {
				studentsList.get(temp).setAskerid(askersList.get(i).getAskerid());
				studentsList.get(temp).setZixunname(askersList.get(i).getAskername());
				temp++;
			}
			flag = updateAllotZXS(studentsList);
			System.out.println(flag==true?true:false);
		}
		//如果学生人数<咨询师人数
		if (xsNum <= zxsNum) {
			for (int i = 0; i < xsNum; i++) {
				studentsList.get(i).setAskerid(askersList.get(i).getAskerid());
				studentsList.get(i).setZixunname(askersList.get(i).getAskername());
			}
			flag = updateAllotZXS(studentsList);
			System.out.println(flag==true?true:false);
		}
		
		//
	}
	
	public boolean updateAllotZXS(List<Students> studentsList) {
		for (Students students : studentsList) {
			return studentsService.update(students)>0?true:false;
		}
		return false;
	}

}
